//定制请求的实例

//导入axios  npm install axios
import axios from 'axios';
//js文件中导入router的方法
import router from '@/router'
//导入token
import { useTokenStore } from '@/stores/token.js';
//组件库消息提示
import { ElMessage } from 'element-plus'
//定义一个变量,记录公共的前缀  ,  baseURL
const baseURL = '/api';
const instance = axios.create({baseURL})


//添加响应拦截器
instance.interceptors.response.use(
    result=>{
        //注意：判断条件要正确，code==='0'则成功
        if(result.data.code===0){ 
            console.log(result.data);
            return result;         
        }
       //alert(result.message?result.message:'服务异常')
       ElMessage.error(result.data.desc?result.data.desc:'服务异常')
       return Promise.reject(result.data)
    },
    err=>{
        //401未登录
        if(err.response.status===401){
            ElMessage.error("请先登录")
            router.push('/login')
        }else{
            // eslint-disable-next-line no-undef
            ElMessage.error('服务异常')
        }
        
        return Promise.reject(err);//异步的状态转化成失败的状态
    }
)

//添加请求拦截器
instance.interceptors.request.use(
    (config)=>{
        const tokenStore = useTokenStore();
        
        if (config.url === '/users/register'||config.url === '/users/send') {
            console.log('请求路径为注册页，无需设置请求头');
            return config;
        }else{
        if(tokenStore.token){
            
            config.headers.Authorization = tokenStore.token
        }else{
            console.log('token为空');
            router.push('./login');
            //ElMessage.warning('请先登录');
        }
        return config;}
    },
    (err)=>{
        Promise.reject(err)
        
    }
)

export default instance;